package cn.yqn.utils.test;

import cn.yqn.utils.ESClientUtil;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class ESTest2 {

    /**
    * @Title: 新建文档
    * @Description: TODO
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/8/17 19:49
    * @Parameters: []
    * @Return void
    */
    @Test
    public void saveTest() {
        // 创建ES搜索对象
        TransportClient client = ESClientUtil.getClient();
        // 创建搜索文档
        IndexRequestBuilder indexRequestBuilder = client.prepareIndex("pethome", "name", "1");
        // 创建集合设置文档内容
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", 1);
        map.put("name", "张三");
        map.put("age", 1);
        map.put("sex", 1);
        IndexResponse indexResponse = indexRequestBuilder.setSource(map).get();
        System.out.println(indexResponse);
        client.close();
    }

    /**
    * @Title: 查询文档
    * @Description: TODO
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/8/17 19:53
    * @Parameters: []
    * @Return void
    */
    @Test
    public void findTest() {
        // 创建ES搜索对象
        TransportClient client = ESClientUtil.getClient();
        // 创建索引
        Map<String, Object> sourceAsMap = client.prepareGet("pethome", "name", "1").get().getSourceAsMap();
        System.out.println(sourceAsMap);
        client.close();
    }

    /**
    * @Title: 修改文档
    * @Description: TODO
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/8/17 19:53
    * @Parameters: []
    * @Return void
    */
    @Test
    public void upTest() {
        TransportClient client = ESClientUtil.getClient();
        UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate("pethome", "name", "1");
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", 1);
        map.put("name", "李四");
        map.put("age", 18);
        map.put("sex", 1);
        UpdateRequestBuilder updateRequestBuilder1 = updateRequestBuilder.setDoc(map);
        System.out.println(updateRequestBuilder1);
        client.close();
    }

    /**
    * @Title: 删除文档
    * @Description: TODO
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/8/17 20:30
    * @Parameters: []
    * @Return void
    */
    @Test
    public void delTest() {
        TransportClient client = ESClientUtil.getClient();
        client.prepareDelete("pethome", "name", "1");
        client.close();
    }

    /**
    * @Title: 批量新增
    * @Description: TODO
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/8/17 20:34
    * @Parameters: []
    * @Return void
    */
    @Test
    public void savesTest() {
        TransportClient client = ESClientUtil.getClient();
        BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
        HashMap<Object, Object> hashMap = new HashMap<>();
        hashMap.put("id", 2);
        hashMap.put("name", "王五");
        hashMap.put("age", 18);
        hashMap.put("sex", 1);
        bulkRequestBuilder.add(client.prepareIndex("shopping", "user", "2").setSource(hashMap));
        Map<String, Object> hashMap2 = new HashMap<>();
        hashMap2.put("id", 3);
        hashMap2.put("name", "赵六");
        hashMap2.put("age", 19);
        hashMap2.put("sex", 1);
        bulkRequestBuilder.add(client.prepareIndex("shopping", "user", "3").setSource(hashMap2));
        BulkResponse bulkItemResponses = bulkRequestBuilder.get();
        Iterator<BulkItemResponse> iterator = bulkItemResponses.iterator();
        while (iterator.hasNext()) {
            BulkItemResponse next = iterator.next();
            System.out.println(next.getResponse());
        }
        client.close();
    }

    @Test
    public void searchTest() {
        TransportClient client = ESClientUtil.getClient();
        SearchRequestBuilder shopping = client.prepareSearch("pethome");
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        // 设置必须的值
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", "五");
        boolQueryBuilder.must(matchQueryBuilder);
        // 开始分词查询
        boolQueryBuilder.filter(QueryBuilders.termQuery("sex", 1));
        // 数据查询范围
        boolQueryBuilder.filter(QueryBuilders.rangeQuery("age").gte(1).lte(12));
        // 设置入boot中
        shopping.setQuery(boolQueryBuilder);
        // 分页数据
        shopping.setSize(2);
        shopping.setFrom(2);
        // 根据年龄排序
        shopping.addSort("age", SortOrder.DESC);
        SearchHit[] hits = shopping.get().getHits().getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsMap());
        }
        client.close();
    }
}
